<?php
session_start();

define('BID','购买了号码');
define('BOMB','您炸了');
define('BOMB_2','对您使用了炸弹');
define('SHIELD','保护了');
define('GLASS','使用了眼睛');

function checkLogin($value){
    if(empty($value)){
        echo '0';
        exit;
    }
}
function checkScore(){
    $sql = "select score from ".DB_PREFIX."user where `id`='".$_SESSION ['user_id']."'";
    return $GLOBALS['db']->getOne($sql);
}
function toolScore($type){
    $game_tool_group = $GLOBALS['db']->getOne("select game_tool_group from ".DB_PREFIX."goods where `id` = '$_GET[good_id]'");
    $sql = "select price from ".DB_PREFIX."game_tools where `tool_name` = '".$type."' and `group` = '".$game_tool_group."'";
    return $GLOBALS['db']->getOne($sql);
}
function bided(){
    $array = array();
    $array = $GLOBALS['db']->getAll("select number from ".DB_PREFIX."game where `user_id` = '".$_SESSION ['user_id']."' and `good_id` = '$_GET[good_id]' and `bomb` is null");
    return $array;
}
function bombed(){
    $array = array();
    $array = $GLOBALS['db']->getAll("select number from ".DB_PREFIX."game where `byuser_id` = '".$_SESSION ['user_id']."' and `good_id` = '$_GET[good_id]' and `bomb` = '1'");
    return $array;
}
function shield(){
    $array = array();
    $array = $GLOBALS['db']->getAll("select number from ".DB_PREFIX."game where `byuser_id` = '".$_SESSION ['user_id']."' and `good_id` = '$_GET[good_id]' and `shield` = '1' and `bomb` is null");
    return $array;
}
function getUserName($userid){
    $sql = "select nickname,user_name from ".DB_PREFIX."user where `id` = '".$userid."'";
    $returnData = $GLOBALS['db']->getAll($sql);
    //print_r($returnData);
    //return $returnData[0][user_name];
    return empty($returnData[0][nickname])?$returnData[0][user_name]:$returnData[0][nickname];
}

if($_GET[action]=='bid'){
    checkLogin($_SESSION ['user_id']);
    if(checkScore()<toolScore($_GET[action])){
        echo '2';
        exit;
    }
    $rows = $GLOBALS['db']->getOne("select count(*) from ".DB_PREFIX."game where `good_id` = '$_GET[good_id]' and `user_id` = '".$_SESSION ['user_id']."' and `number`= '$_GET[number]' and `bid` = '1' and `bomb` is null");
    if(empty($rows)){
        $inseriDate = array('good_id'=>$_GET[good_id],'user_id'=>$_SESSION ['user_id'],'number'=>$_GET[number],'bid'=>'1','dateline'=>a_gmtTime());
        $upDate = array('score'=>checkScore()-toolScore('bid'));
        $GLOBALS['db']->autoExecute(DB_PREFIX."user", addslashes_deep($upDate), 'UPDATE', "id = ".intval($_SESSION ['user_id']));
        $GLOBALS['db']->autoExecute(DB_PREFIX."game", addslashes_deep($inseriDate), 'INSERT');
        $GLOBALS['db']->autoExecute(DB_PREFIX."game_log", addslashes_deep(array('good_id'=>$_GET[good_id],'my_id'=>$_SESSION ['user_id'],'tips'=>'您'.BID.$_GET[number],'dateline'=>a_gmtTime())), 'INSERT');
        $GLOBALS['db']->query("delete from " . DB_PREFIX . "game where `good_id` = '$_GET[good_id]' and `byuser_id` = '".$_SESSION ['user_id']."' and `number`= '$_GET[number]' and `bomb` = '1'" );
        echo  '1';
        exit;
    }else{
        echo '3';
        exit;
    }
}   

if($_GET[action]=='bomb'){
    checkLogin($_SESSION ['user_id']);
    if(checkScore()<toolScore($_GET[action])){
        echo '2';
        exit;
    }
    $upDate = array('score'=>checkScore()-toolScore('bomb'));
    $GLOBALS['db']->autoExecute(DB_PREFIX."user", addslashes_deep($upDate), 'UPDATE', "id = ".intval($_SESSION ['user_id']));
    $where ="where `user_id` <> '".$_SESSION ['user_id']."' and `good_id` = '$_GET[good_id]' and `bomb` is null and `number` = '$_GET[number]'";
    $rows = $GLOBALS['db']->getAll("select * from ".DB_PREFIX."game ".$where."");
    if(!empty($rows[0][id])){
        $killById = $GLOBALS['db']->getOne("select id from ".DB_PREFIX."game ".$where." order by id");
        $getUserid = $GLOBALS['db']->getOne("select user_id from ".DB_PREFIX."game where id = '".$killById."' limit 1");
        if($rows[0][shield]== null or $rows[0][shield]== 0){
            $upDateKilledDate = array('bomb'=>'1','byuser_id'=>$_SESSION ['user_id'],'dateline'=>a_gmtTime());
            $GLOBALS['db']->autoExecute(DB_PREFIX."game", addslashes_deep($upDateKilledDate), 'UPDATE', "id = ".intval($killById));
            $GLOBALS['db']->autoExecute(DB_PREFIX."game_log", addslashes_deep(array('good_id'=>$_GET[good_id],'user_id'=>$getUserid,'my_id'=>$_SESSION ['user_id'],'tips'=>BOMB.getUserName($getUserid),'dateline'=>a_gmtTime())), 'INSERT');
            $GLOBALS['db']->autoExecute(DB_PREFIX."game_log", addslashes_deep(array('good_id'=>$_GET[good_id],'my_id'=>$getUserid,'user_id'=>$_SESSION ['user_id'],'tips'=>getUserName($_SESSION ['user_id']).BOMB_2,'dateline'=>a_gmtTime())), 'INSERT');
        }else{
            $upDateKilledDate = array('shield'=>'NULL','byuser_id'=>$_SESSION ['user_id'],'dateline'=>a_gmtTime());
            $GLOBALS['db']->autoExecute(DB_PREFIX."game", addslashes_deep($upDateKilledDate), 'UPDATE', "id = ".intval($killById));
            $GLOBALS['db']->autoExecute(DB_PREFIX."game_log", addslashes_deep(array('good_id'=>$_GET[good_id],'user_id'=>$getUserid,'my_id'=>$_SESSION ['user_id'],'tips'=>'您拆了'.getUserName($getUserid).'的保护','dateline'=>a_gmtTime())), 'INSERT');
            $GLOBALS['db']->autoExecute(DB_PREFIX."game_log", addslashes_deep(array('good_id'=>$_GET[good_id],'my_id'=>$getUserid,'user_id'=>$_SESSION ['user_id'],'tips'=>getUserName($_SESSION ['user_id']).'拆了您的保护','dateline'=>a_gmtTime())), 'INSERT');
        }
        
        echo '1';
    }else{
        echo '1';
        $GLOBALS['db']->autoExecute(DB_PREFIX."game_log", addslashes_deep(array('good_id'=>$_GET[good_id],'my_id'=>$_SESSION ['user_id'],'tips'=>'您炸了一个空的数字','dateline'=>a_gmtTime())), 'INSERT');
        $upDateKilledDate = array('bomb'=>'1','byuser_id'=>$_SESSION ['user_id'],'dateline'=>a_gmtTime(),'number'=>$_GET[number],'good_id'=>$_GET[good_id]);
        $GLOBALS['db']->autoExecute(DB_PREFIX."game", addslashes_deep($upDateKilledDate), 'INSERT');
        
    }
    exit;
}
if($_GET[action]=='shield'){
    checkLogin($_SESSION ['user_id']);
    if(checkScore()<toolScore($_GET[action])){
        echo '2';
        exit;
    }
    $upDate = array('score'=>checkScore()-toolScore('bomb'));
    $GLOBALS['db']->autoExecute(DB_PREFIX."user", addslashes_deep($upDate), 'UPDATE', "id = ".intval($_SESSION ['user_id']));
    $where ="where `good_id` = '$_GET[good_id]' and `bomb` is null  and `number` = '$_GET[number]' order by id";
    $rows = $GLOBALS['db']->getAll("select * from ".DB_PREFIX."game ".$where."");
    if($rows[0][id] == null){
        $GLOBALS['db']->autoExecute(DB_PREFIX."game_log", addslashes_deep(array('good_id'=>$_GET[good_id],'my_id'=>$_SESSION ['user_id'],'tips'=>'此数字没人购买','dateline'=>a_gmtTime())), 'INSERT');
        echo '10';
        exit;
//        print_r($rows);
    }else{
        if($rows[0][shield] == '1'){
            $GLOBALS['db']->autoExecute(DB_PREFIX."game_log", addslashes_deep(array('good_id'=>$_GET[good_id],'my_id'=>$_SESSION ['user_id'],'tips'=>'购买该数字的玩家已有保护','dateline'=>a_gmtTime())), 'INSERT');
            echo '10';
            exit;
        }else{
            $upDateKilledDate = array('shield'=>'1','byuser_id'=>$_SESSION ['user_id'],'dateline'=>a_gmtTime());
            $GLOBALS['db']->autoExecute(DB_PREFIX."game", addslashes_deep($upDateKilledDate), 'UPDATE', "id = ".intval($rows[0][id]));
            $GLOBALS['db']->query("delete from " . DB_PREFIX . "game where `good_id` = '$_GET[good_id]' and `byuser_id` = '".$_SESSION ['user_id']."' and `number`= '$_GET[number]' and `bomb` = '1'" );
            if($rows[0][user_id] == $_SESSION ['user_id']){
                $GLOBALS['db']->autoExecute(DB_PREFIX."game_log", addslashes_deep(array('good_id'=>$_GET[good_id],'my_id'=>$_SESSION ['user_id'],'tips'=>'您保护了自己,号码是'.$_GET[number],'dateline'=>a_gmtTime())), 'INSERT');
            }else{
                $GLOBALS['db']->autoExecute(DB_PREFIX."game_log", addslashes_deep(array('good_id'=>$_GET[good_id],'my_id'=>$_SESSION ['user_id'],'tips'=>'您保护了'.getUserName($rows[0][user_id]).',号码是'.$_GET[number],'dateline'=>a_gmtTime())), 'INSERT');
                $GLOBALS['db']->autoExecute(DB_PREFIX."game_log", addslashes_deep(array('good_id'=>$_GET[good_id],'my_id'=>$rows[0][user_id],'tips'=>getUserName($_SESSION ['user_id']).'您保护了您,号码是'.$_GET[number],'dateline'=>a_gmtTime())), 'INSERT');
            }
            
            echo '1';
            exit;
        }
    }
    
    exit;
}
if($_GET[action]=='model'){
    $_SESSION['model']=$_POST[model];
    exit;
}
if($_GET[action]=='glass'){
    checkLogin($_SESSION ['user_id']);
    if(checkScore()<toolScore($_GET[action])){
        echo '2';
        exit;
    }
    $upDate = array('score'=>checkScore()-toolScore('bomb'));
    $GLOBALS['db']->autoExecute(DB_PREFIX."user", addslashes_deep($upDate), 'UPDATE', "id = ".intval($_SESSION ['user_id']));
    $data = array();
    $getDate = $GLOBALS['db']->getAll("select * from ".DB_PREFIX."game where `good_id` = '$_GET[good_id]' and `bomb` is null and bid = 1 and user_id <> '".$_SESSION ['user_id']."' and `number` = '$_GET[number]' order by id");
    if(count($getDate) == 0){
        echo '4';
        exit;
    }
    foreach($getDate as $k => $v){
        $data[] = array($getDate[$k]['id']=>getUserName($getDate[$k]['user_id']));
//        $data[] = ;
        
    }
    echo json_encode($data);
    //json_encode 
    exit;
}
function gameInfo($id,$type){
    $sql = $GLOBALS['db']->getOne("select $type from ".DB_PREFIX."game where id = '".$id."' limit 1");
    return $sql;
}

if($_GET[action]=='bombit'){
    checkLogin($_SESSION ['user_id']);
    $upDateKilledDate = array('bomb'=>'1','dateline'=>a_gmtTime());
    $GLOBALS['db']->autoExecute(DB_PREFIX."game", addslashes_deep($upDateKilledDate), 'UPDATE', "id = ".intval($_GET[id]));
    $GLOBALS['db']->autoExecute(DB_PREFIX."game_log", addslashes_deep(array('good_id'=>$_GET[good_id],'my_id'=>$_SESSION ['user_id'],'tips'=>'您炸了'.getUserName(gameInfo($_GET[id],'user_id')),'dateline'=>a_gmtTime())), 'INSERT');
    $GLOBALS['db']->autoExecute(DB_PREFIX."game_log", addslashes_deep(array('good_id'=>$_GET[good_id],'my_id'=>gameInfo($_GET[id],'user_id'),'tips'=>getUserName($_SESSION ['user_id']).'炸了您','dateline'=>a_gmtTime())), 'INSERT');
    echo '1';
    exit;
}

if($_GET[action]=='shieldit'){
    checkLogin($_SESSION ['user_id']);
    $upDateKilledDate = array('shield'=>'1','dateline'=>a_gmtTime());
    $GLOBALS['db']->autoExecute(DB_PREFIX."game", addslashes_deep($upDateKilledDate), 'UPDATE', "id = ".intval($_GET[id]));
    $GLOBALS['db']->autoExecute(DB_PREFIX."game_log", addslashes_deep(array('good_id'=>$_GET[good_id],'my_id'=>$_SESSION ['user_id'],'tips'=>'您保护了'.getUserName(gameInfo($_GET[id],'user_id')),'dateline'=>a_gmtTime())), 'INSERT');
    $GLOBALS['db']->autoExecute(DB_PREFIX."game_log", addslashes_deep(array('good_id'=>$_GET[good_id],'my_id'=>gameInfo($_GET[id],'user_id'),'tips'=>getUserName($_SESSION ['user_id']).'保护了您','dateline'=>a_gmtTime())), 'INSERT');
    echo '1';
    exit;
}

function gamelog(){
    $array = array();
    $array = $GLOBALS['db']->getAll("select * from ".DB_PREFIX."game_log where `my_id` = '".$_SESSION ['user_id']."' order by dateline desc limit 13");

    foreach($array as $k=>$v)
		{
        $array[$k][dateline] = a_toDate($array[$k][dateline],'d日H:i');
                }
    return $array;
}
//print_r(gamelog());
$good_id = $_GET[good_id];
$number = $_GET[number];
$GLOBALS ['tpl']->assign ( "model", empty($_SESSION['model'])?'bid':$_SESSION['model'] );
$GLOBALS ['tpl']->assign ( "bided", bided() );
$GLOBALS ['tpl']->assign ( "bombed", bombed() );
$GLOBALS ['tpl']->assign ( "shield", shield() );
$GLOBALS ['tpl']->assign ( "good_id", $good_id );
$GLOBALS ['tpl']->assign ( "gamelog", gamelog() );
$GLOBALS ['tpl']->assign ( "number", $number );
$GLOBALS ['tpl']->assign ( "score", checkScore() );

$tpl->display("Page/game.moban");
?>